
package linuxbox.util;

import java.util.*;
import java.text.*;

public class StringUtil
{
  private final static String[][] HTMLTable = {
                    {"&", "&amp;"},
                    {"\"","&quot;"},
                    {"<", "&lt;"},
                    {">", "&gt;"},
                    {" ", "&nbsp;"},
                    {"\r", "<br>"} };

 	public static String encodeHtml(String s)
  {
    return replace(s, HTMLTable);
	}

  	/**
	 * replace substrings within string.
     *
     * @param   s
     * @param   sub
     * @param   with
     * @return
	 */
	public static String replace(String s, String sub, String with)
	{
		int c=0;
		int i=s.indexOf(sub,c);
		if (i == -1)
			return s;

		StringBuffer buf = new StringBuffer(s.length()+with.length());

		do
		{
			buf.append(s.substring(c,i));
			buf.append(with);
			c=i+sub.length();
		} while ((i=s.indexOf(sub,c))!=-1);

		if (c<s.length())
			buf.append(s.substring(c,s.length()));

		return buf.toString();
	}

	public static String replace(String s, String[][] pairs)
	{
		int c=0;
    int k=0;
    int i=-1;
    int tmp= -1;
    int indexToReplace = -1;
    for(k=0; k < pairs.length; k++)
    {
      tmp=s.indexOf(pairs[k][0],c);
      if ( tmp!=-1 )
      {
        if ( i == -1 || tmp < i)
        {
          i = tmp;
          indexToReplace = k;
        }
      }
    }
		if (i == -1)
			return s;

		StringBuffer buf = new StringBuffer(s.length()+10);

		do
		{
//System.out.println("i=" + i + " k="+k+" str=" + pairs[indexToReplace][0]);
			buf.append(s.substring(c,i));
			buf.append(pairs[indexToReplace][1]);
			c=i+pairs[indexToReplace][0].length();
      i = -1;
      for(k=0; k < pairs.length; k++)
      {
        tmp=s.indexOf(pairs[k][0],c);
        if ( tmp!=-1 )
        {
          if ( i == -1 || tmp < i)
          {
            i = tmp;
            indexToReplace = k;
          }
        }
      }
		} while (i!=-1);

		if (c<s.length())
			buf.append(s.substring(c,s.length()));

		return buf.toString();
	}

  public static String getDateTimeFormatString(TimeZone tz, String format, Date date)
  {
    SimpleDateFormat dateTimeFormatter = new SimpleDateFormat(format, Locale.US);
    dateTimeFormatter.setCalendar(Calendar.getInstance(tz));
//    dateTimeFormatter.applyPattern(format);
    return dateTimeFormatter.format(date);
  }

  public static String getDateTimeFormatString(TimeZone tz, String format, long date)
  {
    SimpleDateFormat dateTimeFormatter = new SimpleDateFormat(format, Locale.US);
    dateTimeFormatter.setCalendar(Calendar.getInstance(tz));
//    dateTimeFormatter.applyPattern(format);
    Date ddate = new Date(date);
    return dateTimeFormatter.format(ddate);
  }
  public static String TimeZone_list[][] =
      {
        { "GMT+00:00",
          "GMT+00:30",
          "GMT+01:00",
          "GMT+01:30",
          "GMT+02:00",
          "GMT+02:30",
          "GMT+03:00",
          "GMT+03:30",
          "GMT+04:00",
          "GMT+04:30",
          "GMT+05:00",
          "GMT+05:30",
          "GMT+06:00",
          "GMT+06:30",
          "GMT+07:00",
          "GMT+07:30",
          "GMT+08:00",
          "GMT+08:30",
          "GMT+09:00",
          "GMT+09:30",
          "GMT+10:00",
          "GMT+10:30",
          "GMT+11:00",
          "GMT+11:30",
          "GMT+12:00",
          "GMT-12:00",
          "GMT-11:30",
          "GMT-11:00",
          "GMT-10:30",
          "GMT-10:00",
          "GMT-09:30",
          "GMT-09:00",
          "GMT-08:30",
          "GMT-08:00",
          "GMT-07:30",
          "GMT-07:00",
          "GMT-06:30",
          "GMT-06:00",
          "GMT-05:30",
          "GMT-05:00",
          "GMT-04:30",
          "GMT-04:00",
          "GMT-03:30",
          "GMT-03:00",
          "GMT-02:30",
          "GMT-02:00",
          "GMT-01:30",
          "GMT-01:00",
          "GMT-00:30"
      },
          { "GMT +00:00 Britain, Ireland, Portugal, Western Africa",
            "GMT +00:30",
            "GMT +01:00 Western Europe, Central Africa",
            "GMT +01:30",
            "GMT +02:00 Eastern Europe, Eastern Africa",
            "GMT +02:30",
            "GMT +03:00 Russia, Saudi Arabia",
            "GMT +03:30",
            "GMT +04:00 Arabian",
            "GMT +04:30",
            "GMT +05:00 West Asia, Pakistan",
            "GMT +05:30 India",
            "GMT +06:00 Central Asia",
            "GMT +06:30",
            "GMT +07:00 Bangkok, Hanoi, Jakarta",
            "GMT +07:30",
            "GMT +08:00 China, Singapore, Taiwan",
            "GMT +08:30",
            "GMT +09:00 Korea, Japan",
            "GMT +09:30 Central Australia",
            "GMT +10:00 Eastern Australia",
            "GMT +10:30",
            "GMT +11:00 Central Pacific",
            "GMT +11:30",
            "GMT +12:00 Fiji, New Zealand",
            "GMT -12:00 Dateline",
            "GMT -11:30",
            "GMT -11:00 Samoa",
            "GMT -10:30",
            "GMT -10:00 Hawaiian",
            "GMT -09:30",
            "GMT -09:00 Alaska/Pitcairn Islands",
            "GMT -08:30",
            "GMT -08:00 US/Canada/Pacific",
            "GMT -07:30",
            "GMT -07:00 US/Canada/Mountain",
            "GMT -06:30",
            "GMT -06:00 US/Canada/Central",
            "GMT -05:30",
            "GMT -05:00 US/Canada/Eastern, Colombia, Peru",
            "GMT -04:30",
            "GMT -04:00 Bolivia, Western Brazil, Chile,  Atlantic",
            "GMT -03:30 Newfoundland",
            "GMT -03:00 Argentina, Eastern Brazil, Greenland",
            "GMT -02:30",
            "GMT -02:00 Mid-Atlantic",
            "GMT -01:30",
            "GMT -01:00 Azores/Eastern Atlantic",
            "GMT -00:30"
        }
      };


}
